package com.wang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wang.interceptor.LoginInterceptor;
import com.wang.mapper.AddressMapper;
import com.wang.model.AddressDO;
import com.wang.model.LoginUser;
import com.wang.request.AddressAddRequest;
import com.wang.service.AddressService;
import com.wang.vo.AddressVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;

    /**
     * 查询用户所有收货地址
     * @return
     */
    @Override
    public List<AddressVO> ListUserAllAddress() {
        //通过拦截器中的threadLocal获取用户id
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        List<AddressDO> addressDOList = addressMapper.selectList(new QueryWrapper<AddressDO>().eq("user_id", loginUser.getId()));

        //将addressDO转换为addressVO
        List<AddressVO> addressVOList = addressDOList.stream().map(opt -> {
            AddressVO addressVO = new AddressVO();
            BeanUtils.copyProperties(opt, addressVO);
            return addressVO;
        }).collect(Collectors.toList());

        return addressVOList;
    }

    /**
     * 根据id查询地址详情
     * @param addressId
     * @return
     */
    @Override
    public AddressVO detail(long addressId) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        AddressDO addressDO = addressMapper.selectOne(new QueryWrapper<AddressDO>()
                .eq("id", addressId)
                .eq("user_id", loginUser.getId()));
        AddressVO addressVO = new AddressVO();
        BeanUtils.copyProperties(addressDO,addressVO);
        return addressVO;
    }

    /**
     * 添加地址
     * 判断是不是默认地址，如果是再判断数据库中是否有默认地址，
     * 如果有，就将原来的默认地址改为非默认地址，再添加新的默认地址。
     * 如果添加的是非默认地址，直接添加就行。
     * @param addressAddRequest
     */
    @Override
    public void add(AddressAddRequest addressAddRequest) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();//通过拦截器获取用户的id
        AddressDO addressDO = new AddressDO();
        addressDO.setUserId(loginUser.getId());
        addressDO.setCreateTime(new Date());
        BeanUtils.copyProperties(addressAddRequest,addressDO);//拷贝

        //判断新添加的地址是不是默认地址
        if(addressAddRequest.getDefaultStatus() == 1){
            AddressDO addressDO1 = addressMapper.selectOne(new QueryWrapper<AddressDO>().
                    eq("user_id", addressDO.getUserId()).eq("default_status", 1));
            //判断数据库中是否有默认地址
            if(addressDO1 != null){
                //有就将数据库中的默认地址修改为非默认地址
                addressDO1.setDefaultStatus(0);
                addressMapper.updateById(addressDO1);
                return;
            }
        }
            int rows = addressMapper.insert(addressDO);
            log.info("添加地址结果：{},地址详细信息：{}",rows,addressDO);
    }

    /**
     * 删除地址
     * 需要根据当前用户的id和地址id删除地址（不要只根据地址id，因为这样可能导致，其他用户删除了不属于他的地址）
     * @param addressId
     * @return
     */
    @Override
    public int del(long addressId) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        int rows = addressMapper.delete(new QueryWrapper<AddressDO>()
                .eq("id", addressId)
                .eq("user_id", loginUser.getId()));
        return rows;
    }


}
